package com.it.park.controller.expose;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.ArticleService;
import com.it.park.client.service.ResouceFileService;
import com.it.park.client.service.ResourceCommentService;
import com.it.park.client.service.SensitiveWordService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.Article;
import com.it.park.model.ResourceComment;
import com.it.park.model.ResourceFile;
import com.it.park.model.SensitiveWord;
import com.it.park.rms.client.model.User;

@Controller
@RequestMapping("/expose/rescomment")
public class ExposeResourceCommentController {
	private String rootPath=Constant.EXPOSE_PATH;

	@Autowired
	private ArticleService articleService;
	@Autowired
	private ResourceCommentService resourceCommentService;
	@Autowired
	private ResouceFileService resouceFileService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	private Page page = null;
	
	/**
	 * 评论列表
	 * @param pageNow
	 * @param request
	 * @return
	 */
	@RequestMapping("list")
	public String list(@RequestParam("pageNow") Integer pageNow,HttpServletRequest request){
			List<Article> articles =	null;
			Map<String,Object> map = new HashMap<String, Object>();
			Integer totals = articleService.selectTotal(map);
			if(pageNow != null){
				page = new Page(pageNow,totals,10);
			}else{
				page = new Page(1,totals,10);
			}
			map.put("startPos", page.getStartPos());
			map.put("pageSize", page.getPageSize());
			articles = articleService.listRelationUser(map);
			request.setAttribute("totals", totals);
			request.setAttribute("page", page);
			request.setAttribute("articles", articles);
			return rootPath+"article/articleList";
	}
	
	
	/**
	 * 资源评论
	 */
	@RequestMapping("commentResource")
	@ResponseBody
	public JsonObj commentResource(ResourceComment comment,HttpServletRequest request,HttpSession session){
		JsonObj json = new JsonObj();
		User user = (User) session.getAttribute("user");
		if(user != null){
			comment.setUser(user);
			ResourceComment c = resourceCommentService.selectIsExist(comment);
			if(c != null){
				json.setSuccess(false);
				json.setMessage("你已评论过，请勿重复评论!");
				json.setCode(103);
			}else{
				//过滤敏感词
				String commentContent = comment.getCommentContent();
				List<SensitiveWord> words = sensitiveWordService.selectAll();
				for (SensitiveWord word : words) {
					commentContent = commentContent.replaceAll(word.getWord(), "***");
				}
				comment.setCommentContent(commentContent);
				comment.setCommentDate(new Date());
				comment.setUser(user);
				boolean success = resourceCommentService.insertSelective(comment);
				String msg= "评论成功!";
				if(success){
					json.setSuccess(success);
					json.setObject(comment);
					json.setCode(100);
					json.setMessage(msg);
					//评论成功，修改平均评分
					Integer avgScore = resourceCommentService.selectAvgScore(comment.getResourceId());
					ResourceFile record = resouceFileService.selectByPrimaryKey(comment.getResourceId());
					record.setCommentScore(avgScore);
					resouceFileService.updateByPrimaryKeySelective(record);
				}else{
					msg = "评论失败!";
					json.setSuccess(false);
					json.setMessage(msg);
					json.setCode(101);
				}
			}
		}else{
			json.setSuccess(false);
			json.setMessage("对不起，你还未登录，请登录后再操作!");
			json.setCode(102);
		}
		return json;
	}
	
	
	
}
